package com.ofo.ajax;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.google.gson.Gson;
import com.ofo.model.Marks;
import com.ofo.services.IndexActionServices;
import com.opensymphony.xwork2.ActionSupport;

@Scope("prototype")
@Controller
public class IndexAjax extends ActionSupport
		implements ServletRequestAware, SessionAware, ApplicationAware, ServletResponseAware {
	private static final long serialVersionUID = 1L;
	private HttpServletRequest request;
	private Map<String, Object> session;
	private Map<String, Object> application;
	private HttpServletResponse response;
	private Integer marksId;
	private String password;
	private HashMap<String, Object> otherData = new HashMap<String, Object>();// 临时数据储存

	@Resource(name = "indexActionServices")
	private IndexActionServices indexActionServices;

	/*
	 * 删除错误云购记录
	 */
	public void deleteYungouPublish() {
		try {
			// if (bikeAjaxServices.checkBike(bikeid, password, time, isLogin,
			// sig)) {
			// yungouPublishAjaxServices.deleteYungouPublish(goodsID,
			// codePeriod);
			// }
		} catch (Exception e) {
		}
	}

	/*
	 * 获取车牌信息
	 */
	public void getMarks() {
		String loginInfo = null;
		try {
			Marks marks = indexActionServices.getMarks(marksId);
			if (marks != null) {
				otherData.put("resultType", "success");
				otherData.put("marks", marks);
			} else {
				otherData.put("resultType", "error");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		loginInfo = new Gson().toJson(otherData);
		printString(loginInfo);
		// printString(MyStringUtil.changeCharset(loginInfo, "UTF-8",
		// "ISO-8859-1"));
	}

	/*
	 * 添加车牌密码
	 */
	public void addMarks() {
		try {
			// if (bikeAjaxServices.checkBike(bikeid, password, time, isLogin,
			// sig)) {
			// yungouPublishAjaxServices.addGoodsInfo(MyStringUtil.changeCharset(bikeInfo,
			// "ISO-8859-1", "UTF-8"));
			// }
			printString(indexActionServices.addMarks(marksId, password,"admin"));
		} catch (Exception e) {
		}
	}

	/*
	 * 向页面输出
	 */
	private void printString(Object str) {
		PrintWriter out = null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		out.print(str);
		out.close();
	}

	public Integer getMarksId() {
		return marksId;
	}

	public void setMarksId(Integer marksId) {
		this.marksId = marksId;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public Map<String, Object> getApplication() {
		return application;
	}

	public void setApplication(Map<String, Object> application) {
		this.application = application;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	public HashMap<String, Object> getOtherData() {
		return otherData;
	}

	public void setOtherData(HashMap<String, Object> otherData) {
		this.otherData = otherData;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
}
